﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Hevagonsocketsetscrewswithdogpoint
    {
        public static void _Hevagonsocketsetscrewswithdogpoint(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);



            double scale = 10; //放大系数

            double d = Convert.ToDouble(data["d"]) * scale;
            double p = Convert.ToDouble(data["p"]) * scale;
            double dp = Convert.ToDouble(data["dp"]) * scale;
            double df = d - 1.0825 * p;
            double e = Convert.ToDouble(data["e"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double t = Convert.ToDouble(data["t"]) * scale;
            double z = Convert.ToDouble(data["z"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;


            //坐标
            //1
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l + z + 0.4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 0.2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + df / 2);
            Vector2 v5 = new Vector2(v3.X + (d - df) / 2, v3.Y + d / 2);
            Vector2 v7 = new Vector2(v3.X + l - z, v3.Y + dp / 2);
            Vector2 v6 = new Vector2(v7.X - (d - dp) / 2, v5.Y);
            Vector2 v8 = new Vector2(v7.X + z, v7.Y);
            Vector2 v9 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v10 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v11 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v12 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v13 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v14 = new Vector2(v6.X + (v5.Y - v4.Y), v6.Y - (v5.Y - v4.Y));
            Vector2 v15 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v16 = new Vector2(v3.X, v3.Y + e / 2);
            Vector2 v17 = new Vector2(v16.X + t, v16.Y);
            Vector2 v18 = new Vector2(v17.X + e / 2, v3.Y);
            Vector2 v19 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v20 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v21 = new Vector2(v3.X, v3.Y + e / 4);
            Vector2 v22 = new Vector2(v17.X, v21.Y);
            Vector2 v23 = new Vector2(v21.X, v21.Y * -1);
            Vector2 v24 = new Vector2(v22.X, v22.Y * -1);

            Vector2 v25 = new Vector2(v2.X + l, v2.Y);
            Vector2 v26 = new Vector2(v25.X - d / 2 - 0.2, v25.Y);
            Vector2 v27 = new Vector2(v25.X + d / 2 + 0.2, v25.Y);
            Vector2 v28 = new Vector2(v25.X, v25.Y + d / 2 + 0.2);
            Vector2 v29 = new Vector2(v25.X, v25.Y - d / 2 - 0.2);
            Vector2 v30 = new Vector2(v25.X, v25.Y + e / 2);
            Vector2 v31 = fun.Polar(v25, 30 * pi / 180, e / 2);
            Vector2 v32 = new Vector2(v31.X, v31.Y * -1);
            Vector2 v33 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v34 = fun.Polar(v25, -150 * pi / 180, e / 2);
            Vector2 v35 = new Vector2(v34.X, v34.Y * -1);


            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line2, 0.3, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v5));
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            fun.PolyFill(dxf, line3, 0.3, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v4));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            fun.PolyFill(dxf, line4, 0.15, contour);

            Line line5 = new Line(v16, v17);
            line5.Layer = refin;
            line5.Linetype = Linetype.Dashed;
            line5.LinetypeScale = 0.2;
            dxf.AddEntity(line5);

            Line line6 = new Line(v21, v22);
            line6.Layer = refin;
            line6.Linetype = Linetype.Dashed;
            line6.LinetypeScale = 0.2;
            dxf.AddEntity(line6);

            Line line7 = new Line(v24, v23);
            line7.Layer = refin;
            line7.Linetype = Linetype.Dashed;
            line7.LinetypeScale = 0.2;
            dxf.AddEntity(line7);

            Line line8 = new Line(v19, v20);
            line8.Layer = refin;
            line8.Linetype = Linetype.Dashed;
            line8.LinetypeScale = 0.2;
            dxf.AddEntity(line8);

            Line line9 = new Line(v17, v18);
            line9.Layer = refin;
            line9.Linetype = Linetype.Dashed;
            line9.LinetypeScale = 0.2;
            dxf.AddEntity(line9);

            Line line10 = new Line(v18, v20);
            line10.Layer = refin;
            line10.Linetype = Linetype.Dashed;
            line10.LinetypeScale = 0.2;
            dxf.AddEntity(line10);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v17));
            line11.Vertexes.Add(new LwPolylineVertex(v22));
            line11.Vertexes.Add(new LwPolylineVertex(v24));
            line11.Vertexes.Add(new LwPolylineVertex(v20));
            line11.Vertexes[0].Bulge = Math.Tan(-71 * pi / 180 / 4);
            line11.Vertexes[1].Bulge = Math.Tan(-104 * pi / 180 / 4);
            line11.Vertexes[2].Bulge = Math.Tan(-71 * pi / 180 / 4);
            fun.PolyFill(dxf, line11, 0.1, refin);

            Line line12 = new Line(v26, v27);
            line12.Layer = centre;
            line12.Linetype = Linetype.DashDot;
            line12.LinetypeScale = 3;
            dxf.AddEntity(line12);

            Line line13 = new Line(v28, v29);
            line13.Layer = centre;
            line13.Linetype = Linetype.DashDot;
            line13.LinetypeScale = 3;
            dxf.AddEntity(line13);

            fun.drawcircle(dxf, v25, e / 2, 0.3, contour);

            fun.drawcircle(dxf, v25, d / 2, 0.300, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v30));
            line14.Vertexes.Add(new LwPolylineVertex(v31));
            line14.Vertexes.Add(new LwPolylineVertex(v32));
            line14.Vertexes.Add(new LwPolylineVertex(v33));
            line14.Vertexes.Add(new LwPolylineVertex(v34));
            line14.Vertexes.Add(new LwPolylineVertex(v35));
            line14.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, line14, 0.3, contour);





            double height = 2;
            double arrowsize = 1;
            double exline = 2;
            double offset = d;

            fun.dimrot1(dxf, v4, v13, 90, d, 1, height, arrowsize, exline, "df=" + df, "", "");

            fun.dimrot1(dxf, v6, v11, -90, d * 3 / 2, 1, height, arrowsize, exline, "d=" + d, "", "");

            fun.dimrot1(dxf, v8, v9, -90, d / 2, 1, height, arrowsize, exline, "dp=" + dp, "", "");

            fun.dimrot1(dxf, v13, v20, 180, d, 1, height, arrowsize, exline, "t=" + t, "", "");

            fun.dimrot1(dxf, v13, v9, 180, d * 2, 1, height, arrowsize, exline, "l=" + l, "", "");

            fun.dimrot1(dxf, v10, v9, 180, d, 1, height, arrowsize, exline, "z=" + z, "", "");
            
            fun.dimrot1(dxf, v30, v33, -90, d, 1, height, arrowsize, exline, "e=" + e, "", "");

            fun.dimrot1(dxf, v34, v32, 180, d, 1, height, arrowsize, exline, "s=" + s, "", "");

            fun.jdbz2(dxf, new Line(v6, fun.Polar(v6, 135 * pi / 180, 0.1)), new Line(v6, fun.Polar(v6, 180 * pi / 180, 0.1)),offset , height, arrowsize, exline, "≈45°", "", "");

            fun.jdbz1(dxf, new Line(v12, fun.Polar(v12, -45 * pi / 180, 0.1)), new Line(v5, fun.Polar(v5, 45 * pi / 180, 0.1)), d * 2, "90°~120°", 1);



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}